/**
 * 
 */
package vanhoc.android.mediakool;

import java.util.ArrayList;


import vanhoc.android.mediakool.R;
import vanhoc.android.mediakool.custom.adapter.DialogPlaylistAdapter;
import vanhoc.android.mediakool.custom.adapter.PlaylistAdapter;
import vanhoc.android.mediakool.db.DatabaseHelper;
import vanhoc.android.medialcool.helper.AlertDialogManager;
import vanhoc.android.medialcool.item.data.PlaylistItemData;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewStub.OnInflateListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

/**
 * @author Administrator
 *
 */
public class PlaylistActivity extends Activity implements OnItemClickListener, OnItemLongClickListener {
	ListView lvPlaylist;
	
	/**
	 * ds playlist
	 */
	ArrayList<PlaylistItemData> arrPlaylistItem;
	
	//id bai hat dang hat, mac dinh chua co bai hat nao hat thi gan  = 0
	int songPlayingID = 0;
	
	/**
	 * Dialog show options for each item of playlist list
	 */
	Dialog dialogOption;
	
	/**
	 * database object
	 */
	DatabaseHelper databaseHelper = new DatabaseHelper(PlaylistActivity.this);
	
	PlaylistAdapter pAdapter;
	
	private Dialog dialogEdit;
	
	private int _positionSelected;
	
	private int _playlistIdSelected;
	
	private String _playlistTitleSelected;
	
	private String _newPlaylistTitle;
	
	AlertDialogManager alertDialog = new AlertDialogManager();

	ProgressDialog pDialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.playlist);
		
		getIntentData();
		
		initListViewPlaylist();
	}

	private void initListViewPlaylist() {
		lvPlaylist	=	(ListView) findViewById(R.id.lvPlaylist);
		
		//get arrlist playlist
		this.arrPlaylistItem	= getPlaylistItemData();
		
		pAdapter = new PlaylistAdapter(getApplicationContext(), R.id.lvPlaylist, this.arrPlaylistItem);
		//Neu co bai hat nao do dang hat -> lay ve ten album cua no roi gui qua Adapter de chen icon vao album do -> cho biet trong album do co 1 bai hat dang hat
		if (this.songPlayingID != 0) {
			pAdapter.setCurrentPlaylist(this.songPlayingID);
		}
				
		lvPlaylist.setAdapter(pAdapter);
		lvPlaylist.setOnItemClickListener(this);
		lvPlaylist.setOnItemLongClickListener(this);
		
		pAdapter.notifyDataSetChanged();
		lvPlaylist.invalidateViews();
	}

	private void getIntentData() {
		//Khi dang choi nhac o min hinh MusicPlayerActivity, user back lai man hinh TabControlActivity se gui qua Intent id cua bai hat dang choi
		//Con khi moi mo ung dung len(tuc la chua choi nha) thi khong co Intent
		//lay ve id cua bai hat dang choi quan intent (neu co) -> them icon cho biet la co 1 bai hat dang hat trong album nao
		this.songPlayingID	=	 getIntent().getExtras().getInt("songPlayingID");
	}

	private ArrayList<PlaylistItemData> getPlaylistItemData() {
		databaseHelper.openToRead();
		ArrayList<PlaylistItemData> items = databaseHelper.getAllPlaylist();
    	databaseHelper.close();
    	
		return items;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		Intent it = new Intent(getApplicationContext(), SongsActivity.class);
		
		//put id cua playlist sang
		it.putExtra("PLAYLIST_ID", arrPlaylistItem.get(position).getPlaylistId());
		
		it.putExtra("songPlayingID", PlaylistActivity.this.songPlayingID);
		startActivity(it);
	}
	
	//test

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View v, int position,long id) {
		_positionSelected = position;
		_playlistIdSelected = arrPlaylistItem.get(position).getPlaylistId();
		_playlistTitleSelected = arrPlaylistItem.get(position).getPlaylistTitle();
		
		//hien thi cac tuy chon dang listview 
		showOptionList();
		
		return false;
	}

	
	/**
	 * Hien thi list ds cac tuy chon
	 */
	private void showOptionList() {
		dialogOption = new Dialog(PlaylistActivity.this,0);
		dialogOption.setTitle(_playlistTitleSelected);
		dialogOption.setContentView(R.layout.listview);
		dialogOption.show();
		
		final String[] options = getResources().getStringArray(R.array.list_feature_for_playlist);
		
		//list view
		ListView listOption = (ListView) dialogOption.findViewById(R.id.listView);
		
		ArrayAdapter<String>  adapter = new ArrayAdapter<String>(PlaylistActivity.this, android.R.layout.simple_list_item_1, options);
		listOption.setAdapter(adapter);
		
		listOption.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?>  parent, View v, int position,long id) {
				switch (position) {
					case 0:
						edtiPlaylistItem();
						break;
					case 1:
						deletePlaylistItem();
						dialogOption.dismiss();
						initListViewPlaylist();
						break;
					default:
						break;
				}
			}
		});
	}


	/**
	 * Edit 1 playlist nao do
	 * @param pid
	 */
	public void edtiPlaylistItem(){
		dialogEdit = new Dialog(PlaylistActivity.this, 0);
		dialogEdit.setTitle(getResources().getString(R.string.edit));
		dialogEdit.setContentView(R.layout.add_edit_title_layout);
		dialogEdit.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		dialogEdit.show();
		
		//lay ve cac thanh phan cua layout add_playlist_layout thogn qua dialogAddPlaylist
		final EditText txtPlaylistTitle 	= (EditText) dialogEdit.findViewById(R.id.txtTitle);
		txtPlaylistTitle.setText(_playlistTitleSelected);
		
		final Button btnSave 				= (Button) dialogEdit.findViewById(R.id.btnSave);
		Button btnCancel 					= (Button) dialogEdit.findViewById(R.id.btnCancel);
		
		//set onclickListener for btnSave
		btnSave.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				_newPlaylistTitle = txtPlaylistTitle.getText().toString();
				
				if (_newPlaylistTitle.equals(null) || _newPlaylistTitle.equals("")) 
					alertDialog.showAlertDialog(PlaylistActivity.this, "", getResources().getString(R.string.please_enter_playlist_title), false);
				else{
					//kiem tra xem ten playlist user nhap vao da ton tai hay chua.neu chua moi cho tao
					if (checkPlaylistTitle(_newPlaylistTitle)) // neu co roi
						alertDialog.showAlertDialog(PlaylistActivity.this, "", getResources().getString(R.string.existed_playlist_title), false);
					else{
						updatePlaylist(_playlistIdSelected,_newPlaylistTitle);
						dialogEdit.dismiss();
						dialogOption.dismiss();
						initListViewPlaylist();
					}
				}
			}
		});
		
		/*set onclickListener for btnSave*/
		btnCancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dialogEdit.dismiss();
			}
		});
	}
	
	
	/**
	 * Update title cua 1 playlist
	 * @param pid
	 * @param newTitle
	 */
	protected void updatePlaylist(int pid, String newTitle) {
		databaseHelper.openToWrite();
		databaseHelper.updatePlaylistItem(_playlistIdSelected,_newPlaylistTitle);
		databaseHelper.close();
	}

	/**
	 * Kiem tra xem 1 playlist title da ton tai trong bang playlists table chua
	 * @return
	 */
	public Boolean checkPlaylistTitle(String pTitle){
		for (int i = 0; i < this.arrPlaylistItem.size(); i++) {
			if (this.arrPlaylistItem.get(i).getPlaylistTitle().equals(pTitle)) {
				return true;
			}
		}
		
		return false;
	}
	
	
	/**
	 * Delete a playlist
	 * @param pid
	 */
	public void deletePlaylistItem(){
		databaseHelper.openToWrite();
		//xoa record co id = pid trong playlists table
		databaseHelper.deletePlaylistItem(_playlistIdSelected);
		//xoa nhung record co playlist_id = pid trong song_playlist table
		databaseHelper.deleteDataPlaylist(_playlistIdSelected);
		databaseHelper.close();
	}
	
	@Override
	public void onBackPressed() {
		Intent it = new Intent(getApplicationContext(), DashboardActivity.class);
    	startActivity(it);
	}
}
